package com.sohu.tv.utils;

import org.springframework.util.DigestUtils;

/**
 * 授权票据工具类
 * @author yunlongpang
 *
 */
public class OAuthUtil {
	
	private  static final String secret = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	
	/**
	 * 随机生成code
	 * @return
	 */
	public static String code(int length){
        char[] rands = new char[length]; 
        for (int i = 0; i < rands.length; i++) 
        { 
            int rand = (int) (Math.random() * secret.length()); 
            rands[i] = secret.charAt(rand); 
        } 
        StringBuilder sb = new StringBuilder();
        for(int i=0;i<rands.length;i++){
            sb.append(rands[i]);
        }
        return sb.toString();
	}
	/**
	 * 产生不重复的 32 位OpenId
	 * @return
	 */
	public static String openId(int uid,String pid){
			String value = uid + pid;
			String openId = DigestUtils.md5DigestAsHex(value.getBytes());
			return openId;
	}
	/***
	 * 产生不重复的 32 位Token
	 * @return
	 */
	public static String accessToken(){
        return java.util.UUID.randomUUID().toString();
	}
}
